<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('course.shop_id')" type="remoteSelect" v-model="baTable.form.items!.shop_id" prop="shop_id" :input-attr="{ pk: 'xxz_shop.id', field: 'name', 'remote-url': '/admin/Shop/index' ,onRow: chang_shop}" :placeholder="t('Please select field', { field: t('course.shop_id') })" />
                    <FormItem :label="t('course.coach_id')" type="remoteSelect" v-if="baTable.form.items!.shop_id" v-model="baTable.form.items!.coach_id" prop="coach_id" :input-attr="{ pk: 'xxz_coach.id', field: 'name', 'remote-url': '/admin/Coach/index' ,'params':{'shop_id':baTable.form.items!.shop_id}}" :placeholder="t('Please select field', { field: t('course.coach_id') })" />
                    <FormItem :label="t('course.name')" type="string" v-model="baTable.form.items!.name" prop="name" :placeholder="t('Please input field', { field: t('course.name') })" />
                    <FormItem :label="t('course.course_type')" type="radio" v-model="baTable.form.items!.course_type" prop="course_type" :data="{ content: { '1': t('course.course_type 1'), '2': t('course.course_type 2') } }" :placeholder="t('Please select field', { field: t('course.course_type') })" />
                    <FormItem :label="t('course.price')" type="number" prop="price" :input-attr="{ step: 1,min:0 }" v-model.number="baTable.form.items!.price" :placeholder="t('Please input field', { field: t('course.price') })" />
                    <FormItem :label="t('course.vip_price')" type="number" prop="vip_price" :input-attr="{ step: 1,min:0 }" v-model.number="baTable.form.items!.vip_price" :placeholder="t('Please input field', { field: t('course.vip_price') })" />
                    <FormItem :label="t('course.team_min_price')" type="number" prop="team_min_price" :input-attr="{ step: 1,min:0 }" v-model.number="baTable.form.items!.team_min_price" :placeholder="t('Please input field', { field: t('course.team_min_price') })" />
                    <FormItem :label="t('course.image')" type="image" v-model="baTable.form.items!.image" prop="image" />
                    <FormItem :label="t('course.images')" type="images" v-model="baTable.form.items!.images" prop="images" />
<!--                    <FormItem :label="t('course.base_sales')" type="number" prop="base_sales" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.base_sales" :placeholder="t('Please input field', { field: t('course.base_sales') })" />-->
<!--                    <FormItem :label="t('course.sales')" type="number" prop="sales" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.sales" :placeholder="t('Please input field', { field: t('course.sales') })" />-->
                    <FormItem :label="t('course.content')" type="editor" v-model="baTable.form.items!.content" prop="content" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('course.content') })" />
                    <FormItem :label="t('course.status')" type="switch" v-model="baTable.form.items!.status" prop="status" :data="{ content: { '0': t('course.status 0'), '1': t('course.status 1') } }" />
                    <FormItem :label="t('course.weigh')" type="number" prop="weigh" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.weigh" :placeholder="t('Please input field', { field: t('course.weigh') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    shop_id: [buildValidatorData({ name: 'required', title: t('course.shop_id') })],
    coach_id: [buildValidatorData({ name: 'required', title: t('course.coach_id') })],
    name: [buildValidatorData({ name: 'required', title: t('product.name') })],
    course_type: [buildValidatorData({ name: 'required', title: t('course.course_type') })],
    price: [buildValidatorData({ name: 'required', title: t('course.price') })],
    vip_price: [buildValidatorData({ name: 'required', title: t('course.vip_price') })],
    team_min_price: [buildValidatorData({ name: 'required', title: t('course.team_min_price') })],
    image: [buildValidatorData({ name: 'required', title: t('course.image') })],
    images: [buildValidatorData({ name: 'required', title: t('course.images') })],
    base_sales: [buildValidatorData({ name: 'number', title: t('course.base_sales') })],
    sales: [buildValidatorData({ name: 'number', title: t('course.sales') })],
    content: [buildValidatorData({ name: 'editorRequired', title: t('course.content') })],
    create_time: [buildValidatorData({ name: 'date', title: t('course.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('course.update_time') })],
})

const chang_shop = (e) => {
    baTable.form.items.shop_id = '';  // 目的是重新加载一下
    baTable.form.items.coach_id = '';
    setTimeout(function () {
        baTable.form.items.shop_id = e.id;
    },500)

}
</script>

<style scoped lang="scss"></style>
